<script>
  var equalPairs = function (grid) {
    const arr = new Array(grid.length).fill("");
    const o = grid.map(g => {
      return g.join(",");
    })
    grid.forEach((g, i) => {
      grid.forEach((a) => {
        arr[i] += a[i] + ',';
      })
      arr[i] = arr[i].slice(0,-1);
    })
    let ans = 0;
    arr.forEach(a => {
      o.forEach(b => {
        if (a === b) {
          ans++;
        }
      })
    })
    return ans;
  };
  console.log(equalPairs([[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]]));
</script>